// 初始化 app
var myApp = new Framework7();


// 为便于使用，自定义DOM库名字为$$
var $$ = Dom7;

// 添加视图
// Add views

var mainView = myApp.addView('#view-1',{
    dynamicNavbar: true
});
var mainView = myApp.addView('#view-2', {
    dynamicNavbar: true
});
var mainView = myApp.addView('#view-3',{
    dynamicNavbar: true
});
var mainView = myApp.addView('.view-main', {
    // Because we want to use dynamic navbar, we need to enable it for this view:
    dynamicNavbar: true,
});


$$('.open-about').on('click', function () {
    myApp.popup('.popup-about');
    myApp.alert('123');

});


// 方式1：通过页面回调 (推荐):
myApp.onPageInit('Signin', function (page) {
    // 签到日历
    var monthNames = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月' , '9月' , '10月', '11月', '12月'];
    
    var calendarInline = myApp.calendar({
        container: '#calendar-inline-container',
        value: [new Date()],
        weekHeader: false,
        toolbarTemplate: 
            '<div class="toolbar calendar-custom-toolbar">' +
                '<div class="toolbar-inner">' +
                    '<div class="left">' +
                        '<a href="#" class="link icon-only"><i class="icon icon-back"></i></a>' +
                    '</div>' +
                    '<div class="center"></div>' +
                    '<div class="right">' +
                        '<a href="#" class="link icon-only"><i class="icon icon-forward"></i></a>' +
                    '</div>' +
                '</div>' +
            '</div>',
        onOpen: function (p) {
            $$('.calendar-custom-toolbar .center').text(p.currentYear +'年 / ' + monthNames[p.currentMonth]);
            $$('.calendar-custom-toolbar .left .link').on('click', function () {
                calendarInline.prevMonth();
            });
            $$('.calendar-custom-toolbar .right .link').on('click', function () {
                calendarInline.nextMonth();
            });
        },
        onMonthYearChangeStart: function (p) {
            $$('.calendar-custom-toolbar .center').text(p.currentYear +' / ' + monthNames[p.currentMonth]);
        }
    });  


})
myApp.onPageInit('Exchange', function (page) {
})
myApp.onPageInit('Announcement', function (page) {
})
myApp.onPageInit('QAcommunity', function (page) {
})
myApp.onPageInit('Trading', function (page) {
})
myApp.onPageInit('Tokens', function (page) {
})
myApp.onPageInit('IntegralRecord', function (page) {
})
myApp.onPageInit('SigninRecord', function (page) {
})
myApp.onPageInit('GameRecord', function (page) {
})
myApp.onPageInit('PasswordChange', function (page) {
    var timer = null;
    var num = 60;
    var btn = document.getElementById("btn");
    btn.onclick = function(){
        this.disabled = true;
        btn.innerText = "60" + "秒后可再次获取";
        timer = setInterval(function () {
            num--;
            btn.innerText = num + "秒后可再次获取";
            if (num === 0) {
                clearInterval(timer);
                btn.disabled = false;
                btn.innerText = "获取验证码";
                num = 60;
            }
        }, 1000);
    };

})
myApp.onPageInit('Contact', function (page) {
})
myApp.onPageInit('Auestions', function (page) {
})
myApp.onPageInit('Article', function (page) {
})
myApp.onPageInit('ExchangeDetails', function (page) {
})
myApp.onPageInit('Myquestion', function (page) {
})
myApp.onPageInit('LuckyDraw', function (page) {
})
myApp.onPageInit('WalletAddress', function (page) {
    $$('.open-login').on('click', function () {
        myApp.loginScreen();
    });
})
myApp.onPageInit('Mycollection', function (page) {
})
myApp.onPageInit('TokensDetails', function (page) {
})



// 轮播图
var mySwiper1 = myApp.swiper('.swiper-1', {
    pagination:'.swiper-1 .swiper-pagination',
    spaceBetween: 50
});

// $$('#exit').on('click',function(){
//     myApp.alert('确定退出');
// })

$$('.confirm-ok').on('click', function () {
    myApp.confirm('是否确认退出', ['提示']);
});

$$('.copy').on('click',function(){
    myApp.alert('已复制到粘贴板', ['提示']);
})


// 验证码时效

// var timer = null ;
// var num = 60;
// $$('#btn').on('click',function(){
//     this.disabled = true;
//     $$('#btn').text('60'+'秒后可再次获取');
//     timer = setInterval(function(){
//         num--;
//         $$('#btn').text = num + '秒后可再次获取';
//         if(num === 0){
//             clearInterval(timer);
//             $$('#btn').disabled = false;
//             $$('#btn').text = '获取验证码';
//             num = 60;
//         }
//     })
// })

// 上拉刷新
var loading = false;
 
// Last loaded index
var lastIndex = $$('.list-block li').length;
 
// Max items to load
var maxItems = 60;
 
// Append items per load
var itemsPerLoad = 20;
 
// Attach 'infinite' event handler
$$('.infinite-scroll').on('infinite', function () {
 
  // Exit, if loading in progress
  if (loading) return;
 
  // Set loading flag
  loading = true;
 
  // Emulate 1s loading
  setTimeout(function () {
    // Reset loading flag
    loading = false;
 
    if (lastIndex >= maxItems) {
      // Nothing more to load, detach infinite scroll events to prevent unnecessary loadings
      myApp.detachInfiniteScroll($$('.infinite-scroll'));
      // Remove preloader
      $$('.infinite-scroll-preloader').remove();
      return;
    }
 
    // Generate new items HTML
    var html = '';
    for (var i = lastIndex + 1; i <= lastIndex + itemsPerLoad; i++) {
      html += '<li class="item-content"><div class="item-inner"><div class="item-title">Item ' + i + '</div></div></li>';
    }
 
    // Append new items
    $$('.list-block ul').append(html);
 
    // Update last loaded index
    lastIndex = $$('.list-block li').length;
  }, 1000);
});          

